package com.supermap.pisaclient.common;

import android.annotation.SuppressLint;
import android.app.Application;
import android.content.Context;
import android.os.Environment;
import android.os.storage.StorageManager;

import com.supermap.util.util.FileUtil;
import com.supermap.util.util.ResourcesUtil;

/**
 * Copyright (C) 2017 北京超图软件股份有限公司 版权所有
 * <p>
 * 功能描述：application类  作者：李会林
 * <p>
 * 修改人： 修改描述： 修改日期
 */
public class SuperMapAplication extends Application {

    private static Context mContext;
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        ResourcesUtil.init(getResources());
        mContext = getApplicationContext();
        getSdcardpath();
    }

    public static Context getContext() {
        return mContext;
    }

    @SuppressLint("InlinedApi")
    public void getSdcardpath() {
        StorageManager sm = (StorageManager) getSystemService(Context.STORAGE_SERVICE);
        // 读取sd卡的权限
        try {
            String[] paths = (String[]) sm.getClass()
                    .getMethod("getVolumePaths", null).invoke(sm, null);
            if (paths != null) {
                if (paths.length != 0) {
                    for (int i = 0; i < paths.length; i++) {
                        if (!FileUtil.SDCARDPATH.equals(paths[i].trim())) {
                            if (Environment.getExternalStorageState().equals(
                                    Environment.MEDIA_REMOVED)) {
                                FileUtil.SDCARDPATH = paths[i].trim();
                            } else {

                            }
                        }
                    }
                }
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
        FileUtil.INTERNAL_CACHE_DIR = getCacheDir().getAbsolutePath();
        FileUtil.prepareDir();
    }
}
